package ink.lovejinhu.leetcode;

/**
 * @author jinhu
 * created 2021-07-20 13:59
 */
public class Solution_0495 {
    /**
     * @param timeSeries
     * @param duration
     * @return
     */
    public int findPoisonedDuration(int[] timeSeries, int duration) {
        if (duration == 0) return 0;
        int[] ints = new int[timeSeries[timeSeries.length - 1] + duration];
        for (int i = 0; i < timeSeries.length; i++) {
            ints[timeSeries[i]] = Integer.MAX_VALUE; //传染点
        }
        for (int i = 0; i < ints.length; ) {
            if (ints[i] == Integer.MAX_VALUE) {
                for (int j = i; j < i + duration && j < ints.length; j++) {
                    if (ints[j] != Integer.MAX_VALUE) {
                        ints[j]++;
                    }
                }
                i++;
            } else {
                i++;
            }

        }
        int res = 0;
        for (int i = 0; i < ints.length; i++) {
            if (ints[i] > 0) {
                res++;
            }
        }

        return res;
    }

    public static void main(String[] args) {
        Solution_0495 solution_0495 = new Solution_0495();
        int[] time = {1, 2};
        int temp = 2;
        int poisonedDuration = solution_0495.findPoisonedDuration(time, temp);
        System.out.println(poisonedDuration);

    }

    public int findPoisonedDuration1(int[] timeSeries, int duration) {
        int haveTime = 0;
        for (int i = 0; i < timeSeries.length - 1; i++) {
            if (timeSeries[i] + duration <= timeSeries[i + 1]) {
                haveTime = haveTime + duration;
            } else {
                haveTime = haveTime + timeSeries[i + 1] - timeSeries[i];
            }
        }
        return haveTime;
    }
}
